package org.zdc.damasteel;
import nme.display.Sprite;
import nme.geom.Matrix3D;
import nme.geom.Point;
import org.zdc.damasteel.manager.SkinManager;
import org.zdc.damasteel.skin.ISkin;
import org.zdc.damasteel.skin.ComponentSkin;
import org.zdc.damasteel.skin.ISkinable;

class Component extends Sprite,
				implements IComponent,
				implements ISkinable
{
	private static var typeStyles:nme.ObjectHash<IComponent, Dynamic> = new nme.ObjectHash<IComponent, Dynamic>();
	private static var nameStyles:Hash<Dynamic> = new Hash<Dynamic>();
	
	/**
	 * 皮肤
	 */
	private var skin:ComponentSkin;
	
	/**
	 * 关联的皮肤类
	 */
	private var skinClass:Class<ComponentSkin>;
	
	/**
	 * 组件数据
	 */
	private var componentData:Dynamic;
	
	/**
	 * 期望宽
	 */
	private var _preferredWidth:Float;
	public var preferredWidth(get_preferredWidth, set_preferredWidth):Float;
	
	/**
	 * 期望高
	 */
	private var _preferredHeight:Float;
	public var preferredHeight(get_preferredHeight, set_preferredHeight):Float;
	
	/**
	 * 组件父容器
	 */
	private var _cparent:Container;
	public var cparent(get_cparent, null):Container;
	
	/**
	 * 尺寸
	 */
	private var size:Point;
	
	/**
	 * 可用
	 */
	private var valid:Bool;
	
	public function new() 
	{
		super();
		this.mouseEnabled = false;
		this.mouseChildren = false;
		
		if (getSkinClass() != null) {
			skin = Type.createInstance(skinClass, [this]);
		}
		
		valid = true;
		
		installSkin();
	}
	
	/**
	 * 获取期望宽
	 * @return
	 */
	private function get_preferredWidth():Float 
	{
		return _preferredWidth;
	}
	
	/**
	 * 设置期望宽
	 * @param	value
	 */
	private function set_preferredWidth(value:Float):Float 
	{
		return _preferredWidth = value;
	}
	
	/**
	 * 获取期望高
	 * @return
	 */
	private function get_preferredHeight():Float 
	{
		return _preferredHeight;
	}
	
	/**
	 * 设置期望高
	 * @param	value
	 */
	private function set_preferredHeight(value:Float):Float 
	{
		return _preferredHeight = value;
	}
	
	/**
	 * 获取父容器
	 * @return
	 */
	private function get_cparent():Container
	{
		return _cparent;
	}
	
	/**
	 * 获取尺寸
	 * @return
	 */
	public function getSize():Point {
		return size;
	}
	
	/**
	 * 设置尺寸
	 * @param	size
	 */
	public function setSize(size:Point):Void {
		this.size = size;
	}
	
	/**
	 * 布局
	 */
	public function layout():Void {
		
	}
	
	/**
	 * 安装皮肤
	 * @param	skin
	 */
	public function installSkin():Void {
		if (skin == null) {
			return;
		}
		
		skin.installSkin();
	}
	
	/**
	 * 卸载皮肤
	 * @param	skin
	 */
	public function uninstallSkin():Void {
		if (skin == null) {
			return;
		}
		
		skin.uninstallSkin();
	}
	
	/**
	 * 获取关联的皮肤类
	 * @return
	 */
	public function getSkinClass():Class<ComponentSkin> {
		if (skinClass == null) {
			skinClass = SkinManager.getSkinClass(Type.getClass(this));
		}
		return skinClass;
	}
	
	/**
	 * 设置样式
	 * @param	styleName
	 * @param	styleValue
	 */
	public function setStyle(styleName:String, styleValue:Dynamic):Void {
		return skin.setStyle(styleName, styleValue);
	}
	
	/**
	 * 获取样式
	 * @param	styleName
	 * @return
	 */
	public function getStyle(styleName:String):Dynamic {
		return skin.getStyle(styleName);
	}
	
	/**
	 * 获取组件数据
	 * @return
	 */
	public function getData():Dynamic {
		return componentData;
	}
	
	/**
	 * 获取类型样式
	 * @return
	 */
	public static function getTypeStyles():nme.ObjectHash<IComponent, Dynamic> {
		return typeStyles;
	}
	
	/**
	 * 获取命名样式
	 * @return
	 */
	public static function getNameStyles():Hash<Dynamic> {
		return nameStyles;
	}
	
	public function isValid():Bool {
		return valid;
	}
}